﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace CMS_GYM
{
    class clsBangGiaDAO
    {
        #region Attribute
        /// <summary>
        /// Kết nối SQL Server
        /// </summary>
        private CSQLServer sqlServer = new CSQLServer();
        #endregion

        /// <summary>
        /// Lấy danh sách chức năng
        /// </summary>
        public DataTable LayBang()
        {
            string sql = "sp_GetBangGia";
            DataTable table = sqlServer.readData(sql);
            return table;
        }

        public DataTable LayBangNgayTrongTuan()
        {
            string sql = "sp_GetBangNgayTrongTuan";
            DataTable table = sqlServer.readData(sql);
            return table;
        }

        public DataTable LayThongTinBangGia(int BangGiaId)
        {
            string sql = "sp_GetInfoBangGia";
            string[] ParameterColection = new string[1];
            Object[] valueofParameter = new Object[1];
            ParameterColection[0] = "@BangGiaId";
            valueofParameter[0] = BangGiaId;
            DataTable table = sqlServer.readData(sql, ParameterColection, valueofParameter);
            return table;
        }

        private clsBangGiaDTO ChuyenDoi(DataTable table)
        {
            clsBangGiaDTO BangGia = new clsBangGiaDTO();
            if (table.Rows.Count == 1)
            {
                DataRow Dong = table.Rows[0];
                BangGia.BangGiaId = int.Parse(Dong["BangGiaId"].ToString());
                BangGia.NgayTrongTuanId = int.Parse(Dong["NgayTrongTuanId"].ToString());
                BangGia.GioBatDau = Dong["GioBatDau"] == null ? string.Empty : Dong["GioBatDau"].ToString();
                BangGia.GioKetThuc = Dong["GioKetThuc"] == null ? string.Empty : Dong["GioKetThuc"].ToString();
                BangGia.GiaTien = double.Parse( Dong["GiaTien"].ToString());
                //BangGia.TrangThai = bool.Parse(Dong["TrangThai"].ToString()) == true ? 1 : 0;
                //BangGia.IsLock = bool.Parse(Dong["IsLock"].ToString()) == true ? 1 : 0;
                //BangGia.NguoiTaoId = Dong["NguoiTaoId"].ToString() == string.Empty ? 0 : int.Parse(Dong["NguoiTaoId"].ToString());
                //BangGia.NgayTao = Dong["NgayTao"].ToString() == string.Empty ? DateTime.Now : DateTime.Parse(Dong["NgayTao"].ToString());
                //BangGia.NguoiChinhSuaId = Dong["NguoiChinhSuaId"].ToString() == string.Empty ? 0 : int.Parse(Dong["NguoiChinhSuaId"].ToString());
                //BangGia.NgayChinhSua = Dong["NgayChinhSua"].ToString() == string.Empty ? DateTime.Now : DateTime.Parse(Dong["NgayChinhSua"].ToString());
                return BangGia;
            }
            return null;
        }
       /// Thêm chức năng
       /// </summary>
       /// <param name="ChucNang"></param>
       /// <returns></returns>
        public int Them(clsBangGiaDTO BangGia)
        {
            int i = -1;
            string sql = "sp_InsertBangGia";
            string[] ParameterColection = new string[4];
            Object[] valueofParameter = new Object[4];
            ParameterColection[0] = "@NgayTrongTuanId";
            valueofParameter[0] = BangGia.NgayTrongTuanId;
            ParameterColection[1] = "@GioBatDau";
            valueofParameter[1] = BangGia.GioBatDau;
            ParameterColection[2] = "@GioKetThuc";
            valueofParameter[2] = BangGia.GioKetThuc;
            ParameterColection[3] = "@GiaTien";
            valueofParameter[3] = BangGia.GiaTien;
            
            i = sqlServer.writeData(sql, ParameterColection, valueofParameter);
            return i;
        }

        /// <summary>
        /// Sửa thông tin chức năng
        /// </summary>
        /// <param name="BangGia">
        /// MaChucNang  nvarchar(10)
        /// TenChucNang  nvarchar(255)
        /// </param>
        public int Sua(clsBangGiaDTO BangGia)
        {
            int i = -1;
            string sql = "sp_UpdateBangGia";
            string[] ParameterColection = new string[5];
            Object[] valueofParameter = new Object[5];

            ParameterColection[0] = "@BangGiaId";
            valueofParameter[0] = BangGia.BangGiaId;
            ParameterColection[1] = "@NgayTrongTuanId";
            valueofParameter[1] = BangGia.NgayTrongTuanId;
            ParameterColection[2] = "@GioBatDau";
            valueofParameter[2] = BangGia.GioBatDau;
            ParameterColection[3] = "@GioKetThuc";
            valueofParameter[3] = BangGia.GioKetThuc;
            ParameterColection[4] = "@GiaTien";
            valueofParameter[4] = BangGia.GiaTien;
           
            i = sqlServer.writeData(sql, ParameterColection, valueofParameter);
            return i;
        }

        /// <summary>
        /// Xóa chức năng
        /// </summary>
        /// <param name="MaChucNang">Mã chức năng</param>
        public int Xoa(int BangGiaId)
        {
            int i = -1;
            string sql = "sp_DeleteBangGia";
            string[] ParameterColection = new string[1];
            Object[] valueofParameter = new Object[1];
            ParameterColection[0] = "@BangGiaId";
            valueofParameter[0] = BangGiaId;
            i = sqlServer.writeData(sql, ParameterColection, valueofParameter);
            return i;
        }
    }
}
